﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Security.Cryptography;
using System.Text;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Xml;
using System.Net;
using System.Web.Security;
using System.Xml.Serialization;

namespace ManagerVerification
{
    class Category
    {
        public string Name;
        public string ID;
        public int Level;
        public ArrayList childs;

        public Category(string name, string id, int level)
        {
            Name = name;
            ID = id;
            Level = level;
            childs = new ArrayList();
        }
    };

    class ExportElement
    {
        public string Name;
        public string ID;
        public string ParentID;

        public ExportElement(string name, string id, string parentID)
        {
            Name = name;
            ID = id;
            ParentID = parentID;
        }
    };

    public class Navigator
    {
        public int PageNumber { get; set; }

        public Navigator(int num)
        {
            PageNumber = num;
        }
    };

    public partial class Default : System.Web.UI.Page
    {
        private PagedDataSource pds
        {
            get
            {
                if (HttpContext.Current.Session["pds"] == null)
                {
                    HttpContext.Current.Session.Add("pds", new PagedDataSource());
                }
                return ((PagedDataSource)HttpContext.Current.Session["pds"]);
            }
            set
            {
                if (HttpContext.Current.Session["pds"] == null)
                {
                    HttpContext.Current.Session.Add("pds", value);
                }
                else
                {
                    HttpContext.Current.Session["pds"] = value;
                }
            }
        }

        private string CurrentUpload
        {
            get
            {
                if (HttpContext.Current.Application["CurrentUpload"] == null)
                {
                    HttpContext.Current.Application.Add("CurrentUpload", "");

                    return null;
                }
                else
                {
                    return ((string)HttpContext.Current.Application["CurrentUpload"]);
                }
            }
            set
            {
                if (HttpContext.Current.Application["CurrentUpload"] == null)
                {
                    HttpContext.Current.Application.Add("CurrentUpload", value.ToString());
                }
                else
                {
                    HttpContext.Current.Application["CurrentUpload"] = value.ToString();
                }
            }
        }

        private string ChangeWareID
        {
            get
            {
                if (HttpContext.Current.Session["ChangeWareID"] == null)
                {
                    HttpContext.Current.Session.Add("ChangeWareID", "");

                    return null;
                }
                else
                {
                    return ((string)HttpContext.Current.Session["ChangeWareID"]);
                }
            }
            set
            {
                if (HttpContext.Current.Session["ChangeWareID"] == null)
                {
                    HttpContext.Current.Session.Add("ChangeWareID", value.ToString());
                }
                else
                {
                    HttpContext.Current.Session["ChangeWareID"] = value.ToString();
                }
            }
        }

        private string ChangeWareSHCode
        {
            get
            {
                if (HttpContext.Current.Session["ChangeWareSHCode"] == null)
                {
                    HttpContext.Current.Session.Add("ChangeWareSHCode", "");

                    return null;
                }
                else
                {
                    return ((string)HttpContext.Current.Session["ChangeWareSHCode"]);
                }
            }
            set
            {
                if (HttpContext.Current.Session["ChangeWareSHCode"] == null)
                {
                    HttpContext.Current.Session.Add("ChangeWareSHCode", value.ToString());
                }
                else
                {
                    HttpContext.Current.Session["ChangeWareSHCode"] = value.ToString();
                }
            }
        }

        Guid ParentCategoryID = Guid.Parse("5980108d-b8fc-440a-829b-213964ced6a1");//ProductID - Main Category

        protected void Page_Load(object sender, EventArgs e)
        {
            KrishaService service = new KrishaService();

            if (!IsPostBack)
            {
                //HttpContext context = HttpContext.Current;
                //if (context.Request.IsAuthenticated)
                //{
                //    if (string.IsNullOrEmpty(WebUser.UserName))
                //    {
                //        WebUser.UserName = context.User.Identity.Name;
                //    }
                //}
                if (string.IsNullOrEmpty(WebUser.UserName))
                {
                    LoginPanel.Visible = true;
                    Login_Error.Text = "";
                    Tools.Visible = false;
                    ErrorList.Visible = false;
                    ErrorMarker.Visible = false;
                    LoginList.Items.Clear();

                    using (var ta = WebUser.GET_Managers)
                    using (var table = ta.GetData())
                    {
                        foreach (DataRow row in table)
                        {
                            LoginList.Items.Add(new ListItem(row["Name"].ToString(), row["ManagerID"].ToString()));
                        }
                    }
                }
                else
                {
                    LoginPanel.Visible = false;
                    Tools.Visible = true;
                    if (Request.Params["wareid"] != null)
                    {
                        ChangeWareID = Request.Params["wareid"].ToString();
                    }
                    if (Request.Params["shcode"] != null)
                    {
                        ChangeWareSHCode = Request.Params["shcode"].ToString();
                    }
                    else
                    {
                        LoadTable();
                        LoadWarePanel(null);
                    }
                }
            }
        }

        protected void Login_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Password.Text) && LoginList.SelectedItem != null)
            {
                string login = LoginList.SelectedItem.Text;
                Guid id = Guid.Parse(LoginList.SelectedItem.Value);
                string password = Password.Text;

                SHA256 ShaM = new SHA256Managed();
                byte[] encPassword = ShaM.ComputeHash(Encoding.ASCII.GetBytes(password));

                using (var ta = WebUser.GET_ManagerVerification)
                using (var table = ta.GetData(login, encPassword))
                {
                    if (table.Rows.Count > 0)
                    {
                        if (table.Rows[0]["IsSuccessful"].ToString() == "True")
                        {
                            WebUser.UserName = login;
                            WebUser.ManagerID = Guid.Parse(table.Rows[0]["ManagerID"].ToString());
                            /*/                            
                            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                                login,
                                DateTime.Now,
                                DateTime.Now.AddMinutes(360),
                                true,
                                table.Rows[0]["ClientID"].ToString(),
                                FormsAuthentication.FormsCookiePath);
                            string encTicket = FormsAuthentication.Encrypt(ticket);
                            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName);
                            cookie.Value = encTicket;
                            cookie.Secure = FormsAuthentication.RequireSSL;
                            cookie.Domain = FormsAuthentication.CookieDomain;
                            cookie.HttpOnly = true;
                            Response.Cookies.Add(cookie);
                            FormsAuthentication.RedirectFromLoginPage(login, true);
                            /*/

                            Response.Redirect(Request.RawUrl);
                        }
                        else
                        {
                            Login_Error.Text = "Неверный пароль";
                        }
                    }
                    else
                    {
                        Login_Error.Text = "Неверный логин";
                    }
                }
            }
        }

        protected void Logout_Click(object sender, EventArgs e)
        {
            WebUser.UserName = "";
            //FormsAuthentication.SignOut();
            Response.Redirect(Request.RawUrl);
        }

        protected void WaresBtn_Click(object sender, EventArgs e)
        {
            WareTool.Visible = true;
        }

        protected void RecepBtn_Click(object sender, EventArgs e)
        {

        }

        protected void AddBtn_Click(object sender, EventArgs e)
        {
            LoadWarePanel(null);
        }

        #region Producers

        void LoadProducersList()
        {
            ProducersList.Items.Clear();

            ProducersList.Items.Add(new ListItem("", "clear"));

            using (var ta = WebWare.GET_ProducersNames)
            using (var table = ta.GetData())
            {
                for (int i = 0; i < table.Rows.Count; i++)
                    ProducersList.Items.Add(table[i]["Producer"].ToString());
            }
            ProducersList.Items.Add(new ListItem("Добавить производителя", "AddNew"));
        }

        protected void AddProducer_Add_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(AddProducer_ProducerName.Text) && ProducersList.Items.FindByText(AddProducer_ProducerName.Text) == null)
            {
                using (var ta = WebWare.ADD_Producer)
                using (var table = ta.GetData(AddProducer_ProducerName.Text))
                {
                    LoadProducersList();
                    AddProducerPanel.Visible = false;
                    ProducersList.SelectedValue = ProducersList.Items.FindByText(AddProducer_ProducerName.Text).Value;
                    AddProducer_ProducerName.Text = "";
                }
                AddWare.Enabled = true;
            }
        }

        protected void ProducersList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProducersList.SelectedItem.Value == "AddNew")
            {
                AddProducerPanel.Visible = true;
                AddWare.Enabled = false;
            }
            else
            {
                AddProducerPanel.Visible = false;
                AddWare.Enabled = true;
            }
        }

        #endregion
        #region Countries

        void LoadCountriesList()
        {
            CountryList.Items.Clear();

            CountryList.Items.Add(new ListItem("", "clear"));

            using (var ta = WebWare.GET_Countries)
            using (var table = ta.GetData())
            {
                for (int i = 0; i < table.Rows.Count; i++)
                    CountryList.Items.Add(table[i]["Country"].ToString());
            }
            CountryList.Items.Add(new ListItem("Добавить страну", "AddNew"));
        }

        protected void AddCountry_Add_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(AddCountry_CountryName.Text) && CountryList.Items.FindByText(AddCountry_CountryName.Text) == null)
            {
                using (var ta = WebWare.ADD_Country)
                using (var table = ta.GetData(AddCountry_CountryName.Text))
                {
                    LoadCountriesList();
                    AddCountryPanel.Visible = false;
                    CountryList.SelectedValue = CountryList.Items.FindByText(AddCountry_CountryName.Text).Value;
                    AddCountry_CountryName.Text = "";
                }
                AddWare.Enabled = true;
            }
        }

        protected void CountryList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (CountryList.SelectedItem.Value == "AddNew")
            {
                AddCountryPanel.Visible = true;
                AddWare.Enabled = false;
            }
            else
            {
                AddCountryPanel.Visible = false;
                AddWare.Enabled = true;
            }
        }

        #endregion
        #region Shops

        void LoadShopsList()
        {
            ShopList.Items.Clear();

            ShopList.Items.Add(new ListItem("", "clear"));

            using (var ta = WebWare.GET_Shops)
            using (var table = ta.GetData())
            {
                for (int i = 0; i < table.Rows.Count; i++)
                    ShopList.Items.Add(new ListItem(table[i]["Shop"].ToString(), table[i]["ShopID"].ToString()));
            }
            ShopList.Items.Add(new ListItem("Добавить магазин", "AddNew"));
        }

        protected void Add_Shop_Add_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(AddShop_Name.Text) && ShopList.Items.FindByText(AddShop_Name.Text) == null)
            {
                using (var ta = WebWare.ADD_Shop)
                using (var table = ta.GetData(AddShop_Name.Text))
                {
                    LoadShopsList();
                    AddShop_Panel.Visible = false;
                    ShopList.SelectedValue = ShopList.Items.FindByText(AddShop_Name.Text).Value;
                    AddShop_Name.Text = "";
                }
                AddWare.Enabled = true;
            }
        }

        protected void ShopList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ShopList.SelectedItem.Value == "AddNew")
            {
                AddShop_Panel.Visible = true;
                AddWare.Enabled = false;
            }
            else
            {
                AddShop_Panel.Visible = false;
                AddWare.Enabled = true;
            }
        }

        #endregion
        #region ParentCategories

        void LoadParentCategoriesList()
        {
            WareCategoryList.Items.Clear();

            WareCategoryList.Items.Add(new ListItem("", "clear"));

            using (var ta = WebWare.GET_Categories)
            using (var table = ta.GetData(ParentCategoryID))
            {
                for (int i = 0; i < table.Rows.Count; i++)
                    WareCategoryList.Items.Add(new ListItem(table[i]["Name"].ToString(), table[i]["CategoryID"].ToString()));
            }
            WareCategoryList.Items.Add(new ListItem("Добавить категорию", "AddNew"));
        }

        protected void AddWareCategory_Add_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(AddWareCategory_Name.Text) && WareCategoryList.Items.FindByText(AddWareCategory_Name.Text) == null)
            {
                using (var ta = WebWare.ADD_Category)
                using (var table = ta.GetData(AddWareCategory_Name.Text, ParentCategoryID))
                {
                    LoadParentCategoriesList();
                    AddWareCategoryPanel.Visible = false;
                    WareCategoryList.SelectedValue = WareCategoryList.Items.FindByText(AddWareCategory_Name.Text).Value;
                    ChildCategoryPanel.Visible = true;
                    LoadChildCategoriesList(Guid.Parse(WareCategoryList.SelectedValue));
                    AddWareCategory_Name.Text = "";
                }
                AddWare.Enabled = true;
            }
        }

        protected void WareCategoryList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (WareCategoryList.SelectedItem.Value == "AddNew")
            {
                AddWareCategoryPanel.Visible = true;
                ChildCategoryPanel.Visible = false;
                AddWare.Enabled = false;
            }
            else
            {
                AddWareCategoryPanel.Visible = false;
                AddWare.Enabled = true;
                ChildCategoryPanel.Visible = true;
                LoadChildCategoriesList(Guid.Parse(WareCategoryList.SelectedValue));
            }
        }

        #endregion
        #region ChildCategories

        void LoadChildCategoriesList(Guid parentID)
        {
            WareChildCategoryList.Items.Clear();

            WareChildCategoryList.Items.Add(new ListItem("", "clear"));

            using (var ta = WebWare.GET_Categories)
            using (var table = ta.GetData(parentID))
            {
                for (int i = 0; i < table.Rows.Count; i++)
                    WareChildCategoryList.Items.Add(new ListItem(table[i]["Name"].ToString(), table[i]["CategoryID"].ToString()));
                if (table.Rows.Count < 1)
                    WareChildCategoryList.Items.Add(new ListItem("", ""));
            }
            WareChildCategoryList.Items.Add(new ListItem("Добавить категорию", "AddNew"));
        }

        protected void AddWareChildCategory_Add_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(AddWareChildCategory_Name.Text) && WareChildCategoryList.Items.FindByText(AddWareChildCategory_Name.Text) == null)
            {
                using (var ta = WebWare.ADD_Category)
                using (var table = ta.GetData(AddWareChildCategory_Name.Text, Guid.Parse(WareCategoryList.SelectedValue)))
                {
                    LoadChildCategoriesList(Guid.Parse(WareCategoryList.SelectedValue));
                    AddWareChildCategoryPanel.Visible = false;
                    WareChildCategoryList.SelectedValue = WareChildCategoryList.Items.FindByText(AddWareChildCategory_Name.Text).Value;
                    AddWareChildCategory_Name.Text = "";
                }
                AddWare.Enabled = true;
            }
        }

        protected void WareChildCategoryList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (WareChildCategoryList.SelectedItem.Value == "AddNew")
            {
                AddWareChildCategoryPanel.Visible = true;
                AddWare.Enabled = false;
            }
            else
            {
                AddWareChildCategoryPanel.Visible = false;
                AddWare.Enabled = true;
            }
        }

        #endregion
        #region Staff

        void LoadStaff()
        {
            StaffList.Items.Clear();

            if (Session["Staff"] != null)
            {
                Hashtable staff = (Hashtable)(Session["Staff"]);

                foreach (string key in staff.Keys)
                {
                    StaffList.Items.Add(new ListItem(staff[key].ToString(), key));
                }
            }
        }

        void LoadStaffList()
        {
            IngredientsList.Items.Clear();

            IngredientsList.Items.Add(new ListItem("", "clear"));

            using (var ta = WebWare.GET_Ingredients)
            using (var table = ta.GetData())
            {
                for (int i = 0; i < table.Rows.Count; i++)
                    IngredientsList.Items.Add(new ListItem(table[i]["Name"].ToString(), table[i]["IngredientID"].ToString()));
            }
            IngredientsList.Items.Add(new ListItem("Добавить ингредиент", "AddNew"));
        }

        protected void AddIngredient_Add_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(AddIngredient_Name.Text) && IngredientsList.Items.FindByText(AddIngredient_Name.Text) == null &&
                !string.IsNullOrEmpty(AddIngredient_LightValue.Text) && !string.IsNullOrEmpty(AddIngredient_Description.Text) &&
            !string.IsNullOrEmpty(AddIngredient_Comment.Text))
            {
                using (var ta = WebWare.ADD_Ingredient)
                using (var table = ta.GetData(AddIngredient_Name.Text, AddIngredient_TypeList.SelectedItem.Text, decimal.Parse(AddIngredient_LightValue.Text),
                    AddIngredient_Comment.Text, AddIngredient_Description.Text))
                {
                    LoadStaffList();
                    AddIngredientPanel.Visible = false;
                    PushIngredient.Enabled = true;
                    PopIngredient.Enabled = true;
                    IngredientsList.SelectedValue = IngredientsList.Items.FindByText(AddIngredient_Name.Text).Value;
                    AddIngredient_Name.Text = "";
                }
                AddWare.Enabled = true;
            }
        }

        protected void IngredientsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IngredientsList.SelectedItem.Value == "AddNew")
            {
                AddIngredientPanel.Visible = true;
                LoadIngredientTypeList();
                PushIngredient.Enabled = false;
                PopIngredient.Enabled = false;
                AddWare.Enabled = false;
            }
            else
            {
                AddIngredientPanel.Visible = false;
                PushIngredient.Enabled = true;
                PopIngredient.Enabled = true;
                AddWare.Enabled = true;
            }
        }

        #region IngredientTypes

        void LoadIngredientTypeList()
        {
            AddIngredient_TypeList.Items.Clear();

            AddIngredient_TypeList.Items.Add(new ListItem("", "clear"));

            using (var ta = WebWare.GET_IngredientTypes)
            using (var table = ta.GetData())
            {
                for (int i = 0; i < table.Rows.Count; i++)
                    AddIngredient_TypeList.Items.Add(new ListItem(table[i]["IngredientType"].ToString(), table[i]["IngredientTypeID"].ToString()));
            }
            AddIngredient_TypeList.Items.Add(new ListItem("Добавить тип ингредиента", "AddNew"));
        }

        protected void AddIngredient_Type_ADD_Add_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(AddIngredient_Type_ADD_Name.Text) && AddIngredient_TypeList.Items.FindByText(AddIngredient_Type_ADD_Name.Text) == null)
            {
                using (var ta = WebWare.ADD_IngredientType)
                using (var table = ta.GetData(AddIngredient_Type_ADD_Name.Text))
                {
                    LoadIngredientTypeList();
                    AddIngredient_Type_ADDPanel.Visible = false;
                    AddIngredient_Add.Enabled = true;
                    AddIngredient_TypeList.SelectedValue = AddIngredient_TypeList.Items.FindByText(AddIngredient_Type_ADD_Name.Text).Value;
                    AddIngredient_Type_ADD_Name.Text = "";
                }
                AddWare.Enabled = true;
            }
        }

        protected void AddIngredient_TypeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (AddIngredient_TypeList.SelectedItem.Value == "AddNew")
            {
                AddIngredient_Type_ADDPanel.Visible = true;
                AddIngredient_Add.Enabled = false;
                AddWare.Enabled = false;
            }
            else
            {
                AddIngredient_Type_ADDPanel.Visible = false;
                AddIngredient_Add.Enabled = true;
            }
        }

        #endregion

        protected void PushIngredient_Click(object sender, EventArgs e)
        {
            if (Session["Staff"] != null)
            {
                if (!((Hashtable)(Session["Staff"])).ContainsKey(IngredientsList.SelectedItem.Value))
                {
                    ((Hashtable)(Session["Staff"])).Add(IngredientsList.SelectedItem.Value, IngredientsList.SelectedItem.Text);
                }
            }
            else
            {
                Hashtable rr = new Hashtable();
                rr.Add(IngredientsList.SelectedItem.Value, IngredientsList.SelectedItem.Text);
                Session.Add("Staff", rr);
            }
            LoadStaff();
        }

        protected void PopIngredient_Click(object sender, EventArgs e)
        {
            if (Session["Staff"] != null)
            {
                if (((Hashtable)(Session["Staff"])).ContainsKey(StaffList.SelectedItem.Value))
                {
                    ((Hashtable)(Session["Staff"])).Remove(StaffList.SelectedItem.Value);
                }
            }
            LoadStaff();
        }
        #endregion

        void LoadWarePanel(Guid? wareID)
        {
            LoadProducersList();
            LoadCountriesList();
            LoadShopsList();
            LoadParentCategoriesList();
            LoadStaffList();
            LoadStaff();

            AddPanel.Visible = true;

            this.NameBox.Text = string.Empty;
            this.ShortDesc.Text = string.Empty;
            this.FullDesc.Text = string.Empty;
            this.IsHidden.Checked = false;
            this.IsPiece.Checked = false;
            this.SHCode.Text = string.Empty;
            this.Price.Text = string.Empty;
            this.Article.Text = string.Empty;
            this.PriceRed.Text = string.Empty;
            this.NettoWeight.Text = string.Empty;
            this.BruttoWeight.Text = string.Empty;
            this.ValidBefore.Text = string.Empty;
            this.Conditions.Text = string.Empty;
            this.Protein.Text = string.Empty;
            this.Fat.Text = string.Empty;
            this.Carbonhydrates.Text = string.Empty;
            this.EnergyValue.Text = string.Empty;
            if (ProducersList.Items.Count > 0) { ProducersList.SelectedIndex = 0; }
            if (ShopList.Items.Count > 0) { ShopList.SelectedIndex = 0; }
            if (IngredientsList.Items.Count > 0) { IngredientsList.SelectedIndex = 0; }
            if (CountryList.Items.Count > 0) { CountryList.SelectedIndex = 0; }
            if (WareCategoryList.Items.Count > 0) { WareCategoryList.SelectedIndex = 0; }
            if (WareChildCategoryList.Items.Count > 0) { WareChildCategoryList.SelectedIndex = 0; }

            if (wareID != null)
            {
                using (var ta = WebWare.GET_Ware)
                using (var table = ta.GetData(wareID))
                {
                    if (table.Rows.Count > 0)
                    {
                        this.NameBox.Text = table.Rows[0]["WareName"].ToString();
                        this.ShortDesc.Text = (table.Rows[0]["ShortDesc"] == null ? string.Empty : table.Rows[0]["ShortDesc"].ToString());
                        this.FullDesc.Text = (table.Rows[0]["FullDesc"] == null ? string.Empty : table.Rows[0]["FullDesc"].ToString());
                        this.IsHidden.Checked = (table.Rows[0]["IsHidden"] == null ? false : bool.Parse(table.Rows[0]["IsHidden"].ToString()));
                        this.IsPiece.Checked = (table.Rows[0]["IsPiece"] == null ? false : bool.Parse(table.Rows[0]["IsPiece"].ToString()));
                        this.SHCode.Text = (table.Rows[0]["SHCode"] == null ? string.Empty : table.Rows[0]["SHCode"].ToString());
                        this.Price.Text = (table.Rows[0]["PriceMain"] == null ? string.Empty : table.Rows[0]["PriceMain"].ToString());
                        this.Article.Text = (table.Rows[0]["Article"] == null ? string.Empty : table.Rows[0]["Article"].ToString());
                        this.PriceRed.Text = (table.Rows[0]["PriceRed"] == null ? string.Empty : table.Rows[0]["PriceRed"].ToString());
                        this.NettoWeight.Text = (table.Rows[0]["NettoWeight"] == null ? string.Empty : table.Rows[0]["NettoWeight"].ToString());
                        this.BruttoWeight.Text = (table.Rows[0]["BruttoWeight"] == null ? string.Empty : table.Rows[0]["BruttoWeight"].ToString());
                        this.ValidBefore.Text = (table.Rows[0]["ValidBefore"] == null ? string.Empty : table.Rows[0]["ValidBefore"].ToString());
                        this.Conditions.Text = (table.Rows[0]["Conditions"] == null ? string.Empty : table.Rows[0]["Conditions"].ToString());
                        this.Protein.Text = (table.Rows[0]["Protein"] == null ? string.Empty : table.Rows[0]["Protein"].ToString());
                        this.Fat.Text = (table.Rows[0]["Fat"] == null ? string.Empty : table.Rows[0]["Fat"].ToString());
                        this.Carbonhydrates.Text = (table.Rows[0]["Carbonhydrates"] == null ? string.Empty : table.Rows[0]["Carbonhydrates"].ToString());
                        this.EnergyValue.Text = (table.Rows[0]["EnergyValue"] == null ? string.Empty : table.Rows[0]["EnergyValue"].ToString());

                        if (table.Rows[0]["PriceCategory"] != null)
                        {
                            if (PriceCategoryList.Items.FindByText(table.Rows[0]["PriceCategory"].ToString()) != null)
                            {
                                PriceCategoryList.SelectedValue = PriceCategoryList.Items.FindByText(table.Rows[0]["PriceCategory"].ToString()).Value;
                            }
                        }
                        if (table.Rows[0]["Shop"] != null)
                        {
                            if (ShopList.Items.FindByText(table.Rows[0]["Shop"].ToString()) != null)
                            {
                                ShopList.SelectedValue = ShopList.Items.FindByText(table.Rows[0]["Shop"].ToString()).Value;
                            }
                        }

                        if (table.Rows[0]["WareCategory"] != null)
                        {
                            using (var ta1 = WebWare.GET_CategoryByName)
                            using (var table1 = ta1.GetData(table.Rows[0]["WareCategory"].ToString()))
                            {
                                if (table1.Rows.Count > 0)
                                {
                                    Guid parentCategoryID = Guid.Parse(table1.Rows[0]["ParentID"].ToString());

                                    using (var ta2 = WebWare.GET_Category)
                                    using (var table2 = ta2.GetData(parentCategoryID))
                                    {
                                        if (table2.Rows.Count > 0)
                                        {
                                            if (WareCategoryList.Items.FindByText(table2.Rows[0]["Name"].ToString()) != null)
                                            {
                                                WareCategoryList.SelectedValue = WareCategoryList.Items.FindByText(table2.Rows[0]["Name"].ToString()).Value;

                                                WareChildCategoryList.Items.Clear();

                                                WareChildCategoryList.Items.Add(new ListItem("", "clear"));

                                                using (var ta3 = WebWare.GET_Categories)
                                                using (var table3 = ta3.GetData(Guid.Parse(table2.Rows[0]["CategoryID"].ToString())))
                                                {
                                                    for (int i = 0; i < table3.Rows.Count; i++)
                                                        WareChildCategoryList.Items.Add(new ListItem(table3[i]["Name"].ToString(), table3[i]["CategoryID"].ToString()));
                                                    if (table3.Rows.Count < 1)
                                                        WareChildCategoryList.Items.Add(new ListItem("", ""));
                                                }
                                                WareChildCategoryList.Items.Add(new ListItem("Добавить категорию", "AddNew"));
                                            }
                                        }
                                    }
                                }
                                ChildCategoryPanel.Visible = true;
                                if (WareChildCategoryList.Items.FindByText(table.Rows[0]["WareCategory"].ToString()) != null)
                                {
                                    WareChildCategoryList.SelectedValue = WareChildCategoryList.Items.FindByText(table.Rows[0]["WareCategory"].ToString()).Value;
                                }
                            }
                        }

                        if (table.Rows[0]["Producer"] != null)
                        {
                            if (ProducersList.Items.FindByText(table.Rows[0]["Producer"].ToString()) != null)
                            {
                                ProducersList.SelectedValue = ProducersList.Items.FindByText(table.Rows[0]["Producer"].ToString()).Value;
                            }
                        }
                        if (table.Rows[0]["Country"] != null)
                        {
                            if (CountryList.Items.FindByText(table.Rows[0]["Country"].ToString()) != null)
                            {
                                CountryList.SelectedValue = CountryList.Items.FindByText(table.Rows[0]["Country"].ToString()).Value;
                            }
                        }
                        if (table.Rows[0]["StaffListGuid"] != null)
                        {
                            string[] stafflistguid = table.Rows[0]["StaffListGuid"].ToString().Split(';');
                            Guid ingredientID;
                            foreach (string guid in stafflistguid)
                            {
                                if (Guid.TryParse(guid, out ingredientID))
                                {
                                    if (IngredientsList.Items.FindByValue(guid.ToString()) != null)
                                    {
                                        this.StaffList.Items.Add(IngredientsList.Items.FindByValue(guid.ToString()));
                                    }
                                }
                            }
                        }
                    }
                }

                AddWare.Visible = false;
                SaveChanged.Visible = true;
                SaveWareAs.Visible = true;
            }
            else
            {
                AddWare.Visible = true;
                SaveChanged.Visible = false;
                SaveWareAs.Visible = false;
            }
        }

        protected void LoadTable_WithFilters(object sender, EventArgs e)
        {
            Guid? ShopID = null;
            string category = null;
            if (WareTable_CategoriesFilter.SelectedValue != "all")
            {
                if (!string.IsNullOrEmpty(WareTable_CategoriesFilter.SelectedItem.Text))
                {
                    category = WareTable_CategoriesFilter.SelectedItem.Text.TrimStart('-');
                }
            }
            
            using (var ta = WebWare.GET_WaresWithFilters)
            using (var table = ta.GetData(category, ShopID))
            {                
                pds = new PagedDataSource();
                pds.DataSource = table.DefaultView;
                pds.AllowPaging = true;
                pds.PageSize = 20;

                pds.CurrentPageIndex = 0;

                WaresRepeater.DataSource = pds;
                WaresRepeater.DataBind();

                ArrayList navigators = new ArrayList();
                for (int i = 0; i < pds.PageCount; i++)
                {
                    navigators.Add(new Navigator(i + 1));
                }
                NavigateRepeater.DataSource = navigators;
                NavigateRepeater.DataBind();
            }
        }

        void LoadTable()
        {
            WareTable_ShopsFilter.Items.Clear();

            WareTable_ShopsFilter.Items.Add(new ListItem("Все магазины", "all"));

            using (var ta = WebWare.GET_Shops)
            using (var table = ta.GetData())
            {
                for (int i = 0; i < table.Rows.Count; i++)
                    WareTable_ShopsFilter.Items.Add(new ListItem(table[i]["Shop"].ToString(), table[i]["ShopID"].ToString()));
            }

            WareTable_CategoriesFilter.Items.Clear();

            WareTable_CategoriesFilter.Items.Add(new ListItem("Все категории", "all"));

            using (var ta = WebWare.GET_Categories)
            using (var table = ta.GetData(ParentCategoryID))
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    WareTable_CategoriesFilter.Items.Add(new ListItem(table[i]["Name"].ToString(), table[i]["CategoryID"].ToString()));

                    using (var ta2 = WebWare.GET_Categories)
                    using (var table2 = ta.GetData(Guid.Parse(table[i]["CategoryID"].ToString())))
                    {
                        for (int j = 0; j < table2.Rows.Count; j++)
                        {
                            WareTable_CategoriesFilter.Items.Add(new ListItem("----" + table2[j]["Name"].ToString(), table2[j]["CategoryID"].ToString()));
                        }
                    }
                }
            }

            using (var ta = WebWare.GET_Wares)
            using (var table = ta.GetData())
            {
                pds = new PagedDataSource();
                pds.DataSource = table.DefaultView;
                pds.AllowPaging = true;
                pds.PageSize = 20;

                pds.CurrentPageIndex = 0;

                WaresRepeater.DataSource = pds;
                WaresRepeater.DataBind();

                ArrayList navigators = new ArrayList();
                for (int i = 0; i < pds.PageCount; i++)
                {
                    navigators.Add(new Navigator(i + 1));
                }
                NavigateRepeater.DataSource = navigators;
                NavigateRepeater.DataBind();
            }
        }

        protected void NavigatorClick(object sender, EventArgs e)
        {
            int num = int.Parse(((LinkButton)sender).Text.Trim()) - 1;
            pds.CurrentPageIndex = num;

            WaresRepeater.DataSource = pds;
            WaresRepeater.DataBind();
        }

        protected void IdDeletedChanged(object sender, EventArgs e)
        {
            Guid ID = Guid.Parse(((CheckBox)sender).Attributes["wareid"].ToString());

            if (((CheckBox)sender).Checked)
            {
                using (var ta = WebWare.Delete_Ware)
                using (var table = ta.GetData(ID))
                {
                }
            }
            else
            {
                using (var ta = WebWare.UnDelete_Ware)
                using (var table = ta.GetData(ID))
                {
                }
            }
        }

        protected void IdHiddenChanged(object sender, EventArgs e)
        {
            Guid ID = Guid.Parse(((CheckBox)sender).Attributes["wareid"].ToString());

            if (((CheckBox)sender).Checked)
            {
                using (var ta = WebWare.Hide_Ware)
                using (var table = ta.GetData(ID))
                {
                }
            }
            else
            {
                using (var ta = WebWare.UnHide_Ware)
                using (var table = ta.GetData(ID))
                {
                }
            }
        }

        protected void LB_Change_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ChangeWareID))
            {
                LoadWarePanel(Guid.Parse(ChangeWareID));
            }
        }

        protected void TablBtn_Click(object sender, EventArgs e)
        {
            LinkButton button = (LinkButton)sender;
            if (button.Attributes["isHidden"] != null)
            {
                if (bool.Parse(button.Attributes["isHidden"].ToString()))
                {
                    button.Attributes["isHidden"] = false.ToString();
                    button.Text = "Скрыть";

                    LoadTable();

                    WaresTable.Visible = true;
                }
                else
                {
                    button.Attributes["isHidden"] = true.ToString();
                    button.Text = "Показать";

                    WaresTable.Visible = false;
                }
            }
        }

        protected void ClearAddPanelBtn_Click(object sender, EventArgs e)
        {
            LoadWarePanel(null);
        }

        protected void AddWare_Click(object sender, EventArgs e)
        {
            #region Declare

            Guid WareID;
            Guid? GroupID, ShopID, LiveID;
            string WareName, ShortDesc, FullDesc, SHCode, PriceCategory, Producer, Country, WareCategory, Shop, Article, Recipe, Staff, StaffListGuid, ValidBefore, Conditions, DateUpdatePrice;
            decimal PriceMain;
            decimal? PriceRed, NettoWeight, BruttoWeight, Protein, Fat, Carbonhydrates, EnergyValue, LightValue;
            int Counter, Rating;
            bool IsPiece, IsHidden, IsDeleted;

            #endregion

            #region CheckValues

            #region Main

            WareID = Guid.NewGuid();

            #region WareName
            if (!string.IsNullOrEmpty(this.NameBox.Text))
            {
                WareName = this.NameBox.Text;
            }
            else
            {
                this.ErrorMarker.Text = "Не заполнено поле Наименование";
                this.ErrorMarker.Visible = true;
                this.AddWare.Visible = false;
                return;
            }
            #endregion
            #region SHCode
            if (!string.IsNullOrEmpty(this.SHCode.Text))
            {
                SHCode = this.SHCode.Text;
            }
            else
            {
                this.ErrorMarker.Text = "Не заполнено поле Штрихкод";
                this.ErrorMarker.Visible = true;
                this.AddWare.Visible = false;
                return;
            }
            #endregion
            #region Shop
            if (!string.IsNullOrEmpty(this.ShopList.SelectedItem.Value) && this.ShopList.SelectedItem.Value != "AddNew" && this.ShopList.SelectedItem.Value != "create")
            {
                Shop = this.ShopList.SelectedItem.Text;
                ShopID = Guid.Parse(this.ShopList.SelectedItem.Value);
            }
            else
            {
                this.ErrorMarker.Text = "Неверно выбран Магазин";
                this.ErrorMarker.Visible = true;
                this.AddWare.Visible = false;
                return;
            }
            #endregion
            #region WareCategory
            if (!string.IsNullOrEmpty(this.WareChildCategoryList.SelectedValue) && this.WareChildCategoryList.SelectedItem.Value != "AddNew" && this.WareChildCategoryList.SelectedItem.Value != "create")
            {
                WareCategory = this.WareChildCategoryList.SelectedItem.Text;
            }
            else
            {
                this.ErrorMarker.Text = "Неверно выбрана категория товара";
                this.ErrorMarker.Visible = true;
                this.AddWare.Visible = false;
                return;
            }
            #endregion
            #region PriceMain
            if (!decimal.TryParse(this.Price.Text.Replace('.', ','), out PriceMain))
            {
                this.ErrorMarker.Text = "Неверно введена цена";
                this.ErrorMarker.Visible = true;
                this.AddWare.Visible = false;
                return;
            }
            #endregion
            #region PriceCategory
            PriceCategory = PriceCategoryList.SelectedItem.Text;
            #endregion

            #endregion

            #region Other

            DateUpdatePrice = DateTime.Now.ToString();
            IsDeleted = false;
            Rating = 0;
            Counter = 0;

            #region ShortDesc
            ShortDesc = this.ShortDesc.Text;
            #endregion
            #region FullDesc
            FullDesc = this.FullDesc.Text;
            #endregion
            #region IsHidden
            IsHidden = this.IsHidden.Checked;
            #endregion
            #region IsPiece
            IsPiece = this.IsPiece.Checked;
            #endregion
            #region PriceRed
            try
            {
                PriceRed = decimal.Parse(this.PriceRed.Text);
            }
            catch (Exception ex)
            {
                PriceRed = null;
            }
            #endregion
            #region Producer
            if (!string.IsNullOrEmpty(this.ProducersList.SelectedValue) && this.ProducersList.SelectedItem.Value != "AddNew" && this.ProducersList.SelectedItem.Value != "create")
            {
                Producer = this.ProducersList.SelectedItem.Text;
            }
            else
            {
                Producer = "";
            }
            #endregion
            #region Country
            if (!string.IsNullOrEmpty(this.CountryList.SelectedValue) && this.CountryList.SelectedItem.Value != "AddNew" && this.CountryList.SelectedItem.Value != "create")
            {
                Country = this.CountryList.SelectedItem.Text;
            }
            else
            {
                Country = "";
            }
            #endregion
            #region Article
            Article = this.Article.Text;
            #endregion
            #region NettoWeight
            try
            {
                NettoWeight = decimal.Parse(this.NettoWeight.Text);
            }
            catch (Exception ex)
            {
                NettoWeight = null;
            }
            #endregion
            #region BruttoWeight
            try
            {
                BruttoWeight = decimal.Parse(this.BruttoWeight.Text);
            }
            catch (Exception ex)
            {
                BruttoWeight = null;
            }
            #endregion
            #region Protein
            try
            {
                Protein = decimal.Parse(this.Protein.Text);
            }
            catch (Exception ex)
            {
                Protein = null;
            }
            #endregion
            #region Fat
            try
            {
                Fat = decimal.Parse(this.Fat.Text);
            }
            catch (Exception ex)
            {
                Fat = null;
            }
            #endregion
            #region Carbonhydrates
            try
            {
                Carbonhydrates = decimal.Parse(this.Carbonhydrates.Text);
            }
            catch (Exception ex)
            {
                Carbonhydrates = null;
            }
            #endregion
            #region EnergyValue
            try
            {
                EnergyValue = decimal.Parse(this.EnergyValue.Text);
            }
            catch (Exception ex)
            {
                EnergyValue = null;
            }
            #endregion
            #region ValidBefore
            ValidBefore = this.ValidBefore.Text;
            #endregion
            #region Conditions
            Conditions = this.Conditions.Text;
            #endregion

            LiveID = Guid.NewGuid();
            Recipe = string.Empty;

            #region Staff
            Staff = string.Empty;
            StaffListGuid = string.Empty;

            foreach (ListItem item in this.StaffList.Items)
            {
                Staff += item.Text + "<br />";
                StaffListGuid += item.Value + ";";
            }

            if (Staff.Length > 6)
            {
                Staff = Staff.Remove(Staff.Length - 6);
                StaffListGuid = StaffListGuid.Remove(StaffListGuid.Length - 1);
            }
            #endregion
            #region GroupID

            GroupID = null;

            if (BruttoWeight != null && !string.IsNullOrEmpty(WareCategory))
            {
                using (var ta = WebWare.FIND_WareAnalogs)
                using (var table = ta.GetData(WareCategory, BruttoWeight))
                {
                    if (table.Rows.Count > 0)
                    {
                        foreach (DataRow row in table.Rows)
                        {
                            if (!string.IsNullOrEmpty(row["GroupID"].ToString()))
                            {
                                GroupID = Guid.Parse(row["GroupID"].ToString());
                                break;
                            }
                        }
                        if (GroupID == null) { GroupID = Guid.NewGuid(); }
                        foreach (DataRow row in table.Rows)
                        {
                            using (var ta2 = WebWare.SET_GroupID)
                            using (var table2 = ta2.GetData(Guid.Parse(row["WaresID"].ToString()), GroupID))
                            {
                            }
                        }
                    }
                }
            }
            #endregion
            #region LightValue
            ArrayList lightList = new ArrayList();
            LightValue = 0;

            using (var ta = WebWare.GET_Ingredients)
            using (var table = ta.GetData())
            {
                foreach (DataRow row in table.Rows)
                {
                    if (this.StaffList.Items.FindByValue(row["IngredientID"].ToString()) != null)
                    {
                        if (row["LightValue"] != null)
                        {
                            lightList.Add(decimal.Parse(row["LightValue"].ToString()));
                        }
                    }
                }
            }

            //Protein
            if (Protein != null)
            {
                if (Protein < 10) { lightList.Add(90); }
                if (Protein >= 10 && Protein < 20) { lightList.Add(70); }
                if (Protein >= 20 && Protein < 40) { lightList.Add(50); }
                if (Protein >= 40 && Protein < 70) { lightList.Add(30); }
                if (Protein >= 70) { lightList.Add(10); }
            }
            //Fat
            if (Fat != null)
            {
                if (Fat < 10) { lightList.Add(90); }
                if (Fat >= 10 && Fat < 20) { lightList.Add(70); }
                if (Fat >= 20 && Fat < 40) { lightList.Add(50); }
                if (Fat >= 40 && Fat < 70) { lightList.Add(30); }
                if (Fat >= 70) { lightList.Add(10); }
            }
            //Carbonhydrates
            if (Carbonhydrates != null)
            {
                if (Carbonhydrates < 10) { lightList.Add(90); }
                if (Carbonhydrates >= 10 && Carbonhydrates < 20) { lightList.Add(70); }
                if (Carbonhydrates >= 20 && Carbonhydrates < 40) { lightList.Add(50); }
                if (Carbonhydrates >= 40 && Carbonhydrates < 70) { lightList.Add(30); }
                if (Carbonhydrates >= 70) { lightList.Add(10); }
            }

            int count = 0;

            foreach (var value in lightList)
            {
                decimal val = new decimal();
                if (decimal.TryParse(value.ToString(), out val))
                {
                    if (val > 0 && val < 20) { LightValue += val / 5; }
                    if (val >= 20 && val < 40) { LightValue += val / 4; }
                    if (val >= 40 && val < 60) { LightValue += val / 3; }
                    if (val >= 60 && val < 80) { LightValue += val / 2; }
                    if (val >= 80 && val <= 100) { LightValue += val; }
                    if (val > 0 && val <= 100) { count++; }
                }
            }
            if (count != 0)
            {
                LightValue /= count;
            }

            #endregion

            #endregion

            #endregion

            using (var ta = WebWare.ADD_Ware)
            using (var table = ta.GetData(
                    WareID: WareID,
                    WareName: WareName,
                    ShortDesc: ShortDesc,
                    FullDesc: FullDesc,
                    SHCode: SHCode,
                    PriceMain: PriceMain,
                    PriceRed: PriceRed,
                    GroupID: GroupID,
                    NettoWeight: NettoWeight,
                    BruttoWeight: BruttoWeight,
                    Recipe: Recipe,
                    PriceCategory: PriceCategory,
                    Staff: Staff,
                    StaffListGuid: StaffListGuid,
                    ValidBefore: ValidBefore,
                    Conditions: Conditions,
                    Protein: Protein,
                    Fat: Fat,
                    Carbonhydrates: Carbonhydrates,
                    EnergyValue: EnergyValue,
                    Producer: Producer,
                    Country: Country,
                    WareCategory: WareCategory,
                    LightValue: LightValue,
                    DateUpdatePrice: DateUpdatePrice,
                    Counter: Counter,
                    Rating: Rating,
                    ShopID: ShopID,
                    Shop: Shop,
                    LiveID: LiveID,
                    Article: Article,
                    IsPiece: IsPiece,
                    IsHidden: IsHidden,
                    IsDeleted: IsDeleted))
            {
                using (var ta2 = WebWare.ADD_WareAction)
                using (var table2 = ta2.GetData(WareID, WebUser.ManagerID, "create"))
                {
                }

                Response.Redirect(Request.RawUrl);
            }
        }

        protected void SaveChanged_Click(object sender, EventArgs e)
        {

            #region Declare

            Guid ShopID, LiveID;
            Guid? GroupID;
            string WareName, ShortDesc, FullDesc, SHCode, PriceCategory, Producer, Country, WareCategory, Shop, Article, Recipe, Staff, StaffListGuid, ValidBefore, Conditions, DateUpdatePrice;
            decimal PriceMain;
            decimal? PriceRed, NettoWeight, BruttoWeight, Protein, Fat, Carbonhydrates, EnergyValue, LightValue;
            int Counter, Rating;
            bool IsPiece, IsHidden, IsDeleted;

            #endregion

            #region CheckValues

            #region Main

            #region WareName
            if (!string.IsNullOrEmpty(this.NameBox.Text))
            {
                WareName = this.NameBox.Text;
            }
            else
            {
                this.ErrorMarker.Text = "Не заполнено поле Наименование";
                this.ErrorMarker.Visible = true;
                this.AddWare.Visible = false;
                return;
            }
            #endregion
            #region SHCode
            if (!string.IsNullOrEmpty(this.SHCode.Text))
            {
                SHCode = this.SHCode.Text;
            }
            else
            {
                this.ErrorMarker.Text = "Не заполнено поле Штрихкод";
                this.ErrorMarker.Visible = true;
                this.AddWare.Visible = false;
                return;
            }
            #endregion
            #region Shop
            if (!string.IsNullOrEmpty(this.ShopList.SelectedItem.Value) && this.ShopList.SelectedItem.Value != "AddNew" && this.ShopList.SelectedItem.Value != "create")
            {
                Shop = this.ShopList.SelectedItem.Text;
                ShopID = Guid.Parse(this.ShopList.SelectedItem.Value);
            }
            else
            {
                this.ErrorMarker.Text = "Неверно выбран Магазин";
                this.ErrorMarker.Visible = true;
                this.AddWare.Visible = false;
                return;
            }
            #endregion
            #region WareCategory
            if (!string.IsNullOrEmpty(this.WareChildCategoryList.SelectedValue) && this.WareChildCategoryList.SelectedItem.Value != "AddNew" && this.WareChildCategoryList.SelectedItem.Value != "create")
            {
                WareCategory = this.WareChildCategoryList.SelectedItem.Text;
            }
            else
            {
                this.ErrorMarker.Text = "Неверно выбрана категория товара";
                this.ErrorMarker.Visible = true;
                this.AddWare.Visible = false;
                return;
            }
            #endregion
            #region PriceMain
            if (!decimal.TryParse(this.Price.Text.Replace('.', ','), out PriceMain))
            {
                this.ErrorMarker.Text = "Неверно введена цена";
                this.ErrorMarker.Visible = true;
                this.AddWare.Visible = false;
                return;
            }
            #endregion
            #region PriceCategory
            PriceCategory = PriceCategoryList.SelectedItem.Text;
            #endregion

            #endregion

            #region Other

            DateUpdatePrice = DateTime.Now.ToString();
            IsDeleted = false;
            Rating = 0;
            Counter = 0;

            #region ShortDesc
            ShortDesc = this.ShortDesc.Text;
            #endregion
            #region FullDesc
            FullDesc = this.FullDesc.Text;
            #endregion
            #region IsHidden
            IsHidden = this.IsHidden.Checked;
            #endregion
            #region IsPiece
            IsPiece = this.IsPiece.Checked;
            #endregion
            #region PriceRed
            try
            {
                PriceRed = decimal.Parse(this.PriceRed.Text);
            }
            catch (Exception ex)
            {
                PriceRed = null;
            }
            #endregion
            #region Producer
            if (!string.IsNullOrEmpty(this.ProducersList.SelectedValue) && this.ProducersList.SelectedItem.Value != "AddNew" && this.ProducersList.SelectedItem.Value != "create")
            {
                Producer = this.ProducersList.SelectedItem.Text;
            }
            else
            {
                Producer = "";
            }
            #endregion
            #region Country
            if (!string.IsNullOrEmpty(this.CountryList.SelectedValue) && this.CountryList.SelectedItem.Value != "AddNew" && this.CountryList.SelectedItem.Value != "create")
            {
                Country = this.CountryList.SelectedItem.Text;
            }
            else
            {
                Country = "";
            }
            #endregion
            #region Article
            Article = this.Article.Text;
            #endregion
            #region NettoWeight
            try
            {
                NettoWeight = decimal.Parse(this.NettoWeight.Text);
            }
            catch (Exception ex)
            {
                NettoWeight = null;
            }
            #endregion
            #region BruttoWeight
            try
            {
                BruttoWeight = decimal.Parse(this.BruttoWeight.Text);
            }
            catch (Exception ex)
            {
                BruttoWeight = null;
            }
            #endregion
            #region Protein
            try
            {
                Protein = decimal.Parse(this.Protein.Text);
            }
            catch (Exception ex)
            {
                Protein = null;
            }
            #endregion
            #region Fat
            try
            {
                Fat = decimal.Parse(this.Fat.Text);
            }
            catch (Exception ex)
            {
                Fat = null;
            }
            #endregion
            #region Carbonhydrates
            try
            {
                Carbonhydrates = decimal.Parse(this.Carbonhydrates.Text);
            }
            catch (Exception ex)
            {
                Carbonhydrates = null;
            }
            #endregion
            #region EnergyValue
            try
            {
                EnergyValue = decimal.Parse(this.EnergyValue.Text);
            }
            catch (Exception ex)
            {
                EnergyValue = null;
            }
            #endregion
            #region ValidBefore
            ValidBefore = this.ValidBefore.Text;
            #endregion
            #region Conditions
            Conditions = this.Conditions.Text;
            #endregion

            LiveID = Guid.NewGuid();
            Recipe = string.Empty;

            #region Staff
            Staff = string.Empty;
            StaffListGuid = string.Empty;

            foreach (ListItem item in this.StaffList.Items)
            {
                Staff += item.Text + "<br />";
                StaffListGuid += item.Value + ";";
            }

            if (Staff.Length > 6)
            {
                Staff = Staff.Remove(Staff.Length - 6);
                StaffListGuid = StaffListGuid.Remove(StaffListGuid.Length - 1);
            }
            #endregion
            #region GroupID

            GroupID = null;
            using (var ta = WebWare.GET_Ware)
            using (var table = ta.GetData(Guid.Parse(ChangeWareID)))
            {
                if (table.Rows.Count > 0)
                {
                    try
                    {
                        GroupID = Guid.Parse(table.Rows[0]["GroupID"].ToString());
                    }
                    catch (Exception exc) { }
                }
            }

            #endregion
            #region LightValue
            ArrayList lightList = new ArrayList();
            LightValue = 0;

            using (var ta = WebWare.GET_Ingredients)
            using (var table = ta.GetData())
            {
                foreach (DataRow row in table.Rows)
                {
                    if (this.StaffList.Items.FindByValue(row["IngredientID"].ToString()) != null)
                    {
                        if (row["LightValue"] != null)
                        {
                            lightList.Add(decimal.Parse(row["LightValue"].ToString()));
                        }
                    }
                }
            }

            //Protein
            if (Protein != null)
            {
                if (Protein < 10) { lightList.Add(90); }
                if (Protein >= 10 && Protein < 20) { lightList.Add(70); }
                if (Protein >= 20 && Protein < 40) { lightList.Add(50); }
                if (Protein >= 40 && Protein < 70) { lightList.Add(30); }
                if (Protein >= 70) { lightList.Add(10); }
            }
            //Fat
            if (Fat != null)
            {
                if (Fat < 10) { lightList.Add(90); }
                if (Fat >= 10 && Fat < 20) { lightList.Add(70); }
                if (Fat >= 20 && Fat < 40) { lightList.Add(50); }
                if (Fat >= 40 && Fat < 70) { lightList.Add(30); }
                if (Fat >= 70) { lightList.Add(10); }
            }
            //Carbonhydrates
            if (Carbonhydrates != null)
            {
                if (Carbonhydrates < 10) { lightList.Add(90); }
                if (Carbonhydrates >= 10 && Carbonhydrates < 20) { lightList.Add(70); }
                if (Carbonhydrates >= 20 && Carbonhydrates < 40) { lightList.Add(50); }
                if (Carbonhydrates >= 40 && Carbonhydrates < 70) { lightList.Add(30); }
                if (Carbonhydrates >= 70) { lightList.Add(10); }
            }

            int count = 0;

            foreach (var value in lightList)
            {
                decimal val = new decimal();
                if (decimal.TryParse(value.ToString(), out val))
                {
                    if (val > 0 && val < 20) { LightValue += val / 5; }
                    if (val >= 20 && val < 40) { LightValue += val / 4; }
                    if (val >= 40 && val < 60) { LightValue += val / 3; }
                    if (val >= 60 && val < 80) { LightValue += val / 2; }
                    if (val >= 80 && val <= 100) { LightValue += val; }
                    if (val > 0 && val <= 100) { count++; }
                }
            }
            if (count != 0)
            {
                LightValue /= count;
            }

            #endregion

            #endregion

            #endregion

            if (!string.IsNullOrEmpty(ChangeWareID))
            {
                using (var ta = WebWare.UPDATE_Ware)
                using (var table = ta.GetData(
                        WareID: Guid.Parse(ChangeWareID),
                        WareName: WareName,
                        ShortDesc: ShortDesc,
                        FullDesc: FullDesc,
                        SHCode: SHCode,
                        PriceMain: PriceMain,
                        PriceRed: PriceRed,
                        GroupID: GroupID,
                        NettoWeight: NettoWeight,
                        BruttoWeight: BruttoWeight,
                        Recipe: Recipe,
                        PriceCategory: PriceCategory,
                        Staff: Staff,
                        StaffListGuid: StaffListGuid,
                        ValidBefore: ValidBefore,
                        Conditions: Conditions,
                        Protein: Protein,
                        Fat: Fat,
                        Carbonhydrates: Carbonhydrates,
                        EnergyValue: EnergyValue,
                        Producer: Producer,
                        Country: Country,
                        WareCategory: WareCategory,
                        LightValue: LightValue,
                        DateUpdatePrice: DateUpdatePrice,
                        Counter: Counter,
                        Rating: Rating,
                        ShopID: ShopID,
                        Shop: Shop,
                        LiveID: LiveID,
                        Article: Article,
                        IsPiece: IsPiece,
                        IsHidden: IsHidden,
                        IsDeleted: IsDeleted))
                {
                    using (var ta2 = WebWare.ADD_WareAction)
                    using (var table2 = ta2.GetData(Guid.Parse(ChangeWareID), WebUser.ManagerID, "change"))
                    {
                    }

                    Response.Redirect(Request.RawUrl);
                }
            }
        }

        protected void UploadImage(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ChangeWareSHCode))
            {
                if (this.FileUpload1.HasFile)
                {
                    this.FileUpload1.SaveAs(MapPath("~/Cash/" + ChangeWareSHCode + ".png"));
                }
            }
        }


        protected void Add_Shop_Cancel_Click(object sender, EventArgs e)
        {
            if (this.ShopList.Items.Count > 1) { this.ShopList.SelectedIndex = 0; }
            this.AddShop_Panel.Visible = false;
        }
        protected void AddWareCategory_Cancel_Click(object sender, EventArgs e)
        {
            if (this.WareCategoryList.Items.Count > 1) { this.WareCategoryList.SelectedIndex = 0; }
            this.AddWareCategoryPanel.Visible = false;
        }
        protected void AddWareChildCategory_Cancel_Click(object sender, EventArgs e)
        {
            if (this.WareChildCategoryList.Items.Count > 1) { this.WareChildCategoryList.SelectedIndex = 0; }
            this.AddWareChildCategoryPanel.Visible = false;
        }
        protected void AddProducer_Cancel_Click(object sender, EventArgs e)
        {
            if (this.ProducersList.Items.Count > 1) { this.ProducersList.SelectedIndex = 0; }
            this.AddProducerPanel.Visible = false;
        }
        protected void AddCountry_Cancel_Click(object sender, EventArgs e)
        {
            if (this.CountryList.Items.Count > 1) { this.CountryList.SelectedIndex = 0; }
            this.AddCountryPanel.Visible = false;
        }
        protected void AddIngredient_Cancel_Click(object sender, EventArgs e)
        {
            if (this.IngredientsList.Items.Count > 1) { this.IngredientsList.SelectedIndex = 0; }
            this.AddIngredientPanel.Visible = false;
        }
        protected void AddIngredient_Type_ADD_Cancel_Click(object sender, EventArgs e)
        {
            if (this.AddIngredient_TypeList.Items.Count > 1) { this.AddIngredient_TypeList.SelectedIndex = 0; }
            this.AddIngredient_Type_ADDPanel.Visible = false;
        }



        //protected void ImportBtn_Click(object sender, EventArgs e)
        //{
        //    XmlDocument document = new XmlDocument();
        //    document.Load(MapPath("~/export2.xml"));
        //    var nodes = document.ChildNodes[1].ChildNodes;

        //    Hashtable list = new Hashtable();

        //    this.ImportText.Text = "";

        //    #region Categories
        //    //string rootid = nodes[0].ChildNodes[1].InnerText;
        //    ////list.Add(nodes[1].ChildNodes[1].InnerText, new ExportElement(nodes[1].ChildNodes[3].InnerText, nodes[1].ChildNodes[1].InnerText, nodes[1].ChildNodes[2].InnerText));
        //    ////this.ImportText.Text += nodes[1].ChildNodes[3].InnerText + "\n";

        //    ////Random rand = new Random();
        //    //string curName = "";

        //    //foreach (XmlNode node in nodes)
        //    //{
        //    //    if (rootid == node.ChildNodes[2].InnerText && node.ChildNodes[1].InnerText == "Amiro_gen_20780")
        //    //    {
        //    //        //list.Add(node.ChildNodes[1].InnerText, new ExportElement(node.ChildNodes[3].InnerText, node.ChildNodes[1].InnerText, node.ChildNodes[2].InnerText));

        //    //        //this.ImportText.Text += "   " + node.ChildNodes[3].InnerText + "\n";
        //    //        //
        //    //        foreach (XmlNode child in nodes)
        //    //        {
        //    //            if (node.ChildNodes[1].InnerText == child.ChildNodes[2].InnerText)
        //    //            {
        //    //                if (!list.ContainsKey(child.ChildNodes[1].InnerText))
        //    //                {
        //    //                    Guid ID = Guid.NewGuid();

        //    //                    curName = child.ChildNodes[3].InnerText.ToLower();
        //    //                    if (!string.IsNullOrEmpty(curName))
        //    //                    {
        //    //                        curName = curName[0].ToString().ToUpper() + curName.Substring(1);
        //    //                    }

        //    //                    using (var ta = WebWare.ADD_CategoryImport)
        //    //                    using (var table = ta.GetData(ID, child.ChildNodes[1].InnerText, curName, Guid.Parse("5980108d-b8fc-440a-829b-213964ced6a1")))
        //    //                    {
        //    //                    }

        //    //                    list.Add(child.ChildNodes[1].InnerText, new ExportElement(curName, child.ChildNodes[1].InnerText, child.ChildNodes[2].InnerText));

        //    //                    this.ImportText.Text += "      " + curName + "\n";
        //    //                    //--
        //    //                    foreach (XmlNode offer in nodes)
        //    //                    {
        //    //                        if (child.ChildNodes[1].InnerText == offer.ChildNodes[2].InnerText)
        //    //                        {
        //    //                            if (!list.ContainsKey(offer.ChildNodes[1].InnerText))
        //    //                            {
        //    //                                curName = offer.ChildNodes[3].InnerText.ToLower();
        //    //                                if (!string.IsNullOrEmpty(curName))
        //    //                                {
        //    //                                    curName = curName[0].ToString().ToUpper() + curName.Substring(1);
        //    //                                }

        //    //                                using (var ta = WebWare.ADD_CategoryImport)
        //    //                                using (var table = ta.GetData(Guid.NewGuid(), offer.ChildNodes[1].InnerText, curName, ID))
        //    //                                {
        //    //                                }

        //    //                                list.Add(offer.ChildNodes[1].InnerText, new ExportElement(curName, offer.ChildNodes[1].InnerText, offer.ChildNodes[2].InnerText));

        //    //                                this.ImportText.Text += "         " + curName + "\n";
        //    //                                //----
        //    //                                foreach (XmlNode offerchild in nodes)
        //    //                                {
        //    //                                    if (offer.ChildNodes[1].InnerText == offerchild.ChildNodes[2].InnerText)
        //    //                                    {
        //    //                                        if (!list.ContainsKey(offerchild.ChildNodes[1].InnerText))
        //    //                                        {
        //    //                                            curName = offerchild.ChildNodes[3].InnerText.ToLower();
        //    //                                            if (!string.IsNullOrEmpty(curName))
        //    //                                            {
        //    //                                                curName = curName[0].ToString().ToUpper() + curName.Substring(1);
        //    //                                            }

        //    //                                            using (var ta = WebWare.ADD_CategoryImport)
        //    //                                            using (var table = ta.GetData(Guid.NewGuid(), offerchild.ChildNodes[1].InnerText, curName, ID))
        //    //                                            {
        //    //                                            }

        //    //                                            list.Add(offerchild.ChildNodes[1].InnerText, new ExportElement(curName, offer.ChildNodes[2].InnerText, offerchild.ChildNodes[2].InnerText));

        //    //                                            this.ImportText.Text += "         " + curName + "\n";
        //    //                                        }
        //    //                                    }
        //    //                                }
        //    //                                //----
        //    //                            }
        //    //                        }
        //    //                    }
        //    //                    //--
        //    //                }
        //    //            }
        //    //        }
        //    //        //
        //    //    }
        //    //}
        //    #endregion

        //    #region Products

        //    int count = 0, good = 0;

        //    ArrayList badProducts = new ArrayList();

        //    foreach (XmlNode product in nodes)
        //    {
        //        try
        //        {
        //            if (!string.IsNullOrEmpty(product.ChildNodes[9].InnerText))
        //            {
        //                //if (count > 100) break;
        //                count++;
        //                double price = new double(); ;
        //                double.TryParse(product.ChildNodes[12].InnerText.Replace('.', ','), out price);
        //                string shcode = product.ChildNodes[16].InnerText;
        //                string name = product.ChildNodes[9].InnerText;
        //                if (name.IndexOf(')') != -1)
        //                {
        //                    name = name.Substring(name.IndexOf(')') + 1);
        //                    name = name.TrimStart(' ');
        //                }
        //                name = name.Replace("   ", " ").Replace("   ", " ").Replace("  ", " ");
        //                string categoryName = "";
        //                Guid? categoryID = null;
        //                string localID = product.ChildNodes[1].InnerText;
        //                if (!string.IsNullOrEmpty(localID))
        //                {
        //                    using (var ta = WebWare.GET_ImportCategory)
        //                    using (var table = ta.GetData(localID))
        //                    {
        //                        if (table.Rows.Count > 0)
        //                        {
        //                            categoryID = Guid.Parse(table.Rows[0]["CategoryID"].ToString());
        //                        }
        //                    }
        //                }
        //                if (categoryID != null)
        //                {
        //                    using (var ta = WebWare.GET_Category)
        //                    using (var table = ta.GetData(categoryID))
        //                    {
        //                        if (table.Rows.Count > 0)
        //                        {
        //                            categoryName = table.Rows[0]["Name"].ToString();
        //                        }
        //                    }
        //                }
        //                //Weight

        //                decimal? Weight = null;

        //                double localWeight;
        //                string cur = "";

        //                string[] splited = name.Split(new Char[] { ' ', ' ' });
        //                //пробуем
        //                for (int i = 0; i < splited.Count(); i++)
        //                {
        //                    if (double.TryParse(splited[i].Replace('.', ','), out localWeight))
        //                    {
        //                        if (i + 1 < splited.Count())
        //                        {
        //                            cur = splited[i + 1].ToLower().Trim(new Char[] { ' ', '.' });
        //                            if (cur == "гр" || cur == "г" || cur == "кг" || cur == "л" || cur == "мл")
        //                            {
        //                                switch (cur)
        //                                {
        //                                    case "гр": Weight = (decimal)localWeight;
        //                                        break;
        //                                    case "г": Weight = (decimal)localWeight;
        //                                        break;
        //                                    case "мл": Weight = (decimal)localWeight;
        //                                        break;
        //                                    case "кг": Weight = (decimal)localWeight * 1000;
        //                                        break;
        //                                    case "л": Weight = (decimal)localWeight * 1000;
        //                                        break;
        //                                }

        //                                break;
        //                            }
        //                        }
        //                    }
        //                }
        //                //Вторая попытка))
        //                if (Weight == null)
        //                {
        //                    foreach (string str in splited)
        //                    {
        //                        if (str.Length < 2) { continue; }

        //                        cur = str.ToLower().Trim(new Char[] { ' ', '.' });

        //                        if (cur.Substring(cur.Length - 2) == "кг" && double.TryParse(cur.Remove(cur.Length - 2).Replace('.', ','), out localWeight))
        //                        {
        //                            Weight = (decimal)(localWeight * 1000);
        //                            break;
        //                        }

        //                        if (cur.Substring(cur.Length - 2) == "мл" && double.TryParse(cur.Remove(cur.Length - 2).Replace('.', ','), out localWeight))
        //                        {
        //                            Weight = (decimal)(localWeight);
        //                            break;
        //                        }
        //                        if (cur.Substring(cur.Length - 2) == "гр" && double.TryParse(cur.Remove(cur.Length - 2).Replace('.', ','), out localWeight))
        //                        {
        //                            Weight = (decimal)(localWeight);
        //                            break;
        //                        }
        //                        if (cur.Substring(cur.Length - 1) == "л" && double.TryParse(cur.Remove(cur.Length - 1).Replace('.', ','), out localWeight))
        //                        {
        //                            Weight = (decimal)(localWeight * 1000);
        //                            break;
        //                        }
        //                    }
        //                }

        //                //
        //                //
        //                Guid? GroupID = null;

        //                if (BruttoWeight != null && !string.IsNullOrEmpty(categoryName))
        //                {
        //                    using (var ta = WebWare.FIND_WareAnalogs)
        //                    using (var table = ta.GetData(categoryName, Weight))
        //                    {
        //                        if (table.Rows.Count > 0)
        //                        {
        //                            foreach (DataRow row in table.Rows)
        //                            {
        //                                if (!string.IsNullOrEmpty(row["GroupID"].ToString()))
        //                                {
        //                                    GroupID = Guid.Parse(row["GroupID"].ToString());
        //                                    break;
        //                                }
        //                            }
        //                            if (GroupID == null) { GroupID = Guid.NewGuid(); }
        //                            foreach (DataRow row in table.Rows)
        //                            {
        //                                using (var ta2 = WebWare.SET_GroupID)
        //                                using (var table2 = ta2.GetData(Guid.Parse(row["WaresID"].ToString()), GroupID))
        //                                {
        //                                }
        //                            }
        //                        }
        //                    }
        //                }
        //                //

        //                //string imageURL = product.ChildNodes[13].InnerText;
        //                //if (!string.IsNullOrEmpty(imageURL) && !string.IsNullOrEmpty(shcode))
        //                //{
        //                //    if (imageURL.IndexOf('.') >= 0)
        //                //    {
        //                //        imageURL = imageURL.Remove(imageURL.IndexOf('.'));
        //                //    }

        //                //    DirectoryInfo dir = new DirectoryInfo(@"C:\Users\Иван\Desktop\Проект\www.vezet.me\_mod_files\ce_images\eshop\generated");
        //                //    var images = dir.GetFiles("*" + imageURL + "*");
        //                //    if (images.Count() > 0)
        //                //    {
        //                //        long hight = 0;
        //                //        int hi = 0;
        //                //        for (int i = 0; i < images.Count(); i++)
        //                //        {
        //                //            if (images[i].Length > hight)
        //                //            {
        //                //                hi = i;
        //                //                hight = images[i].Length;
        //                //            }
        //                //        }
        //                //        images[hi].CopyTo(MapPath("~/Cash/" + shcode + ".png"));
        //                //    }
        //                //}


        //                //ImportText.Text += "Name:        " + name +
        //                //                    "\nCategory:    " + categoryName +
        //                //                    "\nPrice:       " + price.ToString() +
        //                //                    "\nSHCode:      " + shcode + "\n\n\n";

        //                using (var ta = WebWare.ADD_Ware)
        //                using (var table = ta.GetData(
        //                        WareID: Guid.NewGuid(),
        //                        WareName: name,
        //                        ShortDesc: "",
        //                        FullDesc: "",
        //                        SHCode: shcode,
        //                        PriceMain: (decimal)price,
        //                        PriceRed: null,
        //                        GroupID: GroupID,
        //                        NettoWeight: null,
        //                        BruttoWeight: Weight,
        //                        Recipe: null,
        //                        PriceCategory: "Стандарт",
        //                        Staff: "",
        //                        StaffListGuid: "",
        //                        ValidBefore: "",
        //                        Conditions: "",
        //                        Protein: null,
        //                        Fat: null,
        //                        Carbonhydrates: null,
        //                        EnergyValue: null,
        //                        Producer: "",
        //                        Country: "",
        //                        WareCategory: categoryName,
        //                        LightValue: null,
        //                        DateUpdatePrice: DateTime.Now.ToString(),
        //                        Counter: 0,
        //                        Rating: 0,
        //                        ShopID: Guid.Parse("115533f9-42e7-401d-8057-02b44df2bf92"),
        //                        Shop: "Окей",
        //                        LiveID: Guid.NewGuid(),
        //                        Article: null,
        //                        IsPiece: true,
        //                        IsHidden: true,
        //                        IsDeleted: false))
        //                { }
        //            }
        //        }
        //        catch (Exception exc)
        //        {
        //            System.IO.StreamWriter file = new System.IO.StreamWriter(MapPath("~/bad/" + badProducts.Count.ToString() + ".txt"));
        //            XmlSerializer serialize = new XmlSerializer(typeof(XmlNode));
        //            serialize.Serialize(file, product);
        //            file.Close();
        //            badProducts.Add(product);
        //        }
        //    }
        //    ImportText.Text = "Bad products: " + badProducts.Count.ToString();
        //    //ImportText.Text += "\n" + (((double)good / (double)count) * 100).ToString() + "%";


        //    #endregion

        //    #region wtf??
        //    /*XmlDocument document = new XmlDocument();
        //    document.Load(MapPath("~/export.xml"));

        //    var offers = document.ChildNodes[1].ChildNodes[0].ChildNodes[5];

        //    ImportText.Text = "";

        //    for (int i = 0; i < offers.ChildNodes.Count && i < 20; i++)
        //    {
        //        string loaclID = offers.ChildNodes[i].Attributes["id"].Value;
        //        bool isHidden = bool.Parse(offers.ChildNodes[i].Attributes["available"].Value);
        //        string name = offers.ChildNodes[i].SelectSingleNode("name").InnerText;
        //        double price = double.Parse(offers.ChildNodes[i].SelectSingleNode("price").InnerText.Replace('.', ','));
        //        if (name.IndexOf(')') != -1)
        //        {
        //            name = name.Substring(name.IndexOf(')') + 1);
        //            name = name.TrimStart(' ');
        //        }
        //        string categoryName = "";
        //        Guid? categoryID = null;
        //        string localID = offers.ChildNodes[i].SelectSingleNode("categoryId").InnerText;
        //        if (!string.IsNullOrEmpty(loaclID))
        //        {
        //            using (var ta = WebWare.GET_ImportCategory)
        //            using (var table = ta.GetData(localID))
        //            {
        //                if (table.Rows.Count > 0)
        //                {
        //                    categoryID = Guid.Parse(table.Rows[0]["CategoryID"].ToString());
        //                }
        //            }
        //        }
        //        if (categoryID != null)
        //        {
        //            using (var ta = WebWare.GET_Category)
        //            using (var table = ta.GetData(categoryID))
        //            {
        //                if (table.Rows.Count > 0)
        //                {
        //                    categoryName = table.Rows[0]["Name"].ToString();
        //                }
        //            }
        //        }

        //        try
        //        {
        //            string imageURL = offers.ChildNodes[i].SelectSingleNode("picture").InnerText;
        //            if (!string.IsNullOrEmpty(imageURL))
        //            {
        //                WebClient client = new WebClient();
        //                Stream stream = client.OpenRead(imageURL);
        //                Bitmap bitmap = new Bitmap(stream);
        //                stream.Flush();
        //                stream.Close();

        //                if (bitmap != null)
        //                {
        //                    bitmap.Save(MapPath("~/Cash/" + name + ".png"), ImageFormat.Png);
        //                }
        //            }
        //        }
        //        catch (Exception exc) { ImportText.Text += "UploadError -- "; }

        //        ImportText.Text += name + "    " + price.ToString() + "\n";

        //        //using (var ta = WebWare.ADD_Ware)
        //        //using (var table = ta.GetData(
        //        //        WareName: name,
        //        //        ShortDesc: "",
        //        //        FullDesc: "",
        //        //        SHCode: "",
        //        //        PriceMain: (decimal)price,
        //        //        PriceRed: null,
        //        //        GroupID: null,
        //        //        NettoWeight: null,
        //        //        BruttoWeight: null,
        //        //        Recipe: null,
        //        //        PriceCategory: "Стандарт",
        //        //        Staff: "",
        //        //        StaffListGuid: "",
        //        //        ValidBefore: "",
        //        //        Conditions: "",
        //        //        Protein: null,
        //        //        Fat: null,
        //        //        Carbonhydrates: null,
        //        //        EnergyValue: null,
        //        //        Producer: "",
        //        //        Country: "",
        //        //        WareCategory: categoryName,
        //        //        LightValue: null,
        //        //        DateUpdatePrice: null,
        //        //        Counter: 0,
        //        //        Rating: 0,
        //        //        ShopID: Guid.Parse("115533f9-42e7-401d-8057-02b44df2bf92"),
        //        //        Shop: "ОКей",
        //        //        LiveID: null,
        //        //        Article: null,
        //        //        IsPiece: true,
        //        //        IsHidden: isHidden,
        //        //        IsDeleted: false))
        //        //{ }
        //    }

        //    //Categories
        //    /*
        //    var categories = document.ChildNodes[1].ChildNodes[0].ChildNodes[4];
        //    string rootid = categories.ChildNodes[0].Attributes["id"].Value;
        //    ImportText.Text = "";

        //    ArrayList listt = new ArrayList();
        //    listt.Add(rootid);

        //    int count = 1;
        //    Category root = new Category("root", rootid, 0);

        //    for (int i = 1; i < categories.ChildNodes.Count; i++)
        //    {
        //        string parentID = categories.ChildNodes[i].Attributes["parentId"].Value;

        //        if (parentID == rootid)
        //        {
        //            root.childs.Add(new Category(categories.ChildNodes[i].InnerText, categories.ChildNodes[i].Attributes["id"].Value, 1));
        //            listt.Add(categories.ChildNodes[i].Attributes["id"].Value);
        //        }
        //    }

        //    count += root.childs.Count;
        //    foreach (Category cat in root.childs)
        //    {
        //        //ImportText.Text += cat.Name + "\n";

        //        for (int i = 1; i < categories.ChildNodes.Count; i++)
        //        {
        //            string parentID = categories.ChildNodes[i].Attributes["parentId"].Value;

        //            if (parentID == cat.ID)
        //            {
        //                cat.childs.Add(new Category(categories.ChildNodes[i].InnerText, categories.ChildNodes[i].Attributes["id"].Value, 2));

        //                listt.Add(categories.ChildNodes[i].Attributes["id"].Value);

        //                //count++;
        //                //ImportText.Text += "   " + categories.ChildNodes[i].InnerText + "\n";

        //                for (int j = 1; j < categories.ChildNodes.Count; j++)
        //                {
        //                    string parentID0 = categories.ChildNodes[j].Attributes["parentId"].Value;

        //                    if (parentID0 == categories.ChildNodes[i].Attributes["id"].Value)
        //                    {
        //                        cat.childs.Add(new Category(categories.ChildNodes[j].InnerText, categories.ChildNodes[j].Attributes["id"].Value, 2));
        //                        //count++;
        //                        //ImportText.Text += "      " + categories.ChildNodes[j].InnerText + "\n";
        //                    }
        //                }

        //            }
        //        }
        //    }
        //    //ImportText.Text += count.ToString() + " - " + categories.ChildNodes.Count.ToString() +"\n\n\n";

        //    //for (int i = 1; i < categories.ChildNodes.Count; i++)
        //    //{
        //    //    string ID = categories.ChildNodes[i].Attributes["id"].Value;
        //    //    if (!listt.Contains(ID))
        //    //    {
        //    //        ImportText.Text+=categories.ChildNodes[i].InnerText+"\n";
        //    //    }
        //    //}

        //    //Response.Redirect(Request.RawUrl);
        //    count = 0;
        //    foreach (Category cat in root.childs)
        //    {
        //        Guid ID = Guid.NewGuid();

        //        using (var ta = WebWare.ADD_CategoryImport)
        //        using (var table = ta.GetData(ID, cat.ID, cat.Name, Guid.Parse("5980108d-b8fc-440a-829b-203964ced6b0")))
        //        {
        //        }

        //        ImportText.Text += cat.Name + "\n";
        //        count++;

        //        foreach (Category child in cat.childs)
        //        {
        //            Guid ID2 = Guid.NewGuid();

        //            using (var ta = WebWare.ADD_CategoryImport)
        //            using (var table = ta.GetData(ID2, child.ID, child.Name, ID))
        //            {
        //            }

        //            ImportText.Text += "   " + child.Name + "\n";
        //            count++;
        //        }
        //    }
        //    ImportText.Text += count.ToString();
        //     * */
        //    #endregion
        //}
    }
}